package org.example.wx.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.common.utils.ParamFilter;
import org.example.db.entity.Goods;
import org.example.db.entity.GoodsAttribute;
import org.example.db.entity.GoodsSpecification;
import org.example.db.mapper.GoodsAttributeMapper;
import org.example.db.mapper.GoodsMapper;
import org.example.db.mapper.GoodsSpecificationMapper;
import org.example.wx.dto.GoodsDetailDto;
import org.example.wx.dto.GoodsGeneralDto;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class WxGoodsService {

    GoodsMapper goodsMapper;
    GoodsAttributeMapper goodsAttributeMapper;
    GoodsSpecificationMapper goodsSpecificationMapper;

    public WxGoodsService(GoodsMapper goodsMapper, GoodsAttributeMapper goodsAttributeMapper, GoodsSpecificationMapper goodsSpecificationMapper) {
        this.goodsMapper = goodsMapper;
        this.goodsAttributeMapper = goodsAttributeMapper;
        this.goodsSpecificationMapper = goodsSpecificationMapper;
    }

    public IPage<Goods> getPageGoodsByCategoryId(Page<Goods> page, Long categoryId, String sort, String order){

        ParamFilter.orderFilter(order);
        ParamFilter.sortFilter(sort);

        boolean isAsc = true;
        if(order.equals("desc")){
            isAsc = false;
        }

        QueryWrapper<Goods> query = Wrappers.query();

        query.eq("category_id", categoryId);
        query.orderBy(true, isAsc, sort);

        return goodsMapper.selectPage(page, query);
    }

    public GoodsDetailDto getGoodsDetail(Long goodsId){

        Goods goods = goodsMapper.selectById(goodsId);
        List<GoodsAttribute> goodsAttributeList = goodsAttributeMapper.selectList(
                Wrappers.lambdaQuery(GoodsAttribute.class).eq(GoodsAttribute::getGoodsId, goodsId));

        List<GoodsSpecification> goodsSpecificationList = goodsSpecificationMapper.selectList(Wrappers.lambdaQuery(GoodsSpecification.class).eq(GoodsSpecification::getGoodsId, goodsId));


        return new GoodsDetailDto(goods, goodsAttributeList, goodsSpecificationList);
    }

    public GoodsGeneralDto getGoodsGeneral(Long goodsId, Long goodsSpecId){

        GoodsSpecification goodsSpecification = goodsSpecificationMapper.selectById(goodsSpecId);
        Goods goods = goodsMapper.selectById(goodsId);
        GoodsGeneralDto goodsGeneralDto = new GoodsGeneralDto();
        goodsGeneralDto.setSpecificationId(goodsSpecification.getId());
        goodsGeneralDto.setGoodsName(goods.getName());
        goodsGeneralDto.setPictureUrl(goodsSpecification.getPictureUrl());
        goodsGeneralDto.setSpecificationValue(goodsSpecification.getValue());
        goodsGeneralDto.setPrice(goodsSpecification.getPrice());

        return goodsGeneralDto;
    }

    public GoodsGeneralDto getGoodsGeneral(Long goodsSpecId, Integer number){

        GoodsSpecification goodsSpecification = goodsSpecificationMapper.selectById(goodsSpecId);
        Goods goods = goodsMapper.selectById(goodsSpecification.getGoodsId());
        GoodsGeneralDto goodsGeneralDto = new GoodsGeneralDto();
        goodsGeneralDto.setSpecificationId(goodsSpecification.getId());
        goodsGeneralDto.setGoodsName(goods.getName());
        goodsGeneralDto.setNumber(number);
        goodsGeneralDto.setPictureUrl(goodsSpecification.getPictureUrl());
        goodsGeneralDto.setSpecificationValue(goodsSpecification.getValue());
        goodsGeneralDto.setPrice(goodsSpecification.getPrice());


        return goodsGeneralDto;
    }


}
